package com.email.impl;

import com.email.EmailConfig;
import com.email.IEmailService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2006-1-16
 * Time: 10:39:22
 * To change this template use File | Settings | File Templates.
 */
public class EmailService implements IEmailService {
    protected Log log = LogFactory.getLog(EmailService.class);

    public void senderSySEmail(String toEmail, String subject, String body) {
        SimpleEmail email = new SimpleEmail();
        try {
            email.addTo(toEmail);
            email.setHostName(EmailConfig.getHost());
            email.setFrom(EmailConfig.getSysFromEmail(), EmailConfig.getSysFromName());
            email.setSubject(subject);
            email.setMsg(body);
            email.send();
        } catch (EmailException e) {
            log.error(e.getMessage(), e);
        }
    }

    public void senderInvitationalEmail(String toEmail, String friendId, String userName) {
        SimpleEmail email = new SimpleEmail();
        try {
            email.addTo(toEmail);
            email.setHostName(EmailConfig.getHost());
            email.setFrom(EmailConfig.getSysFromEmail(), EmailConfig.getSysFromName());
            email.setSubject(EmailConfig.getMessageFormatContent(EmailConfig.getInvitationalEmail(), new String[]{userName}));
            email.setMsg(EmailConfig.getInvitationalURL() + friendId);
            email.send();
        } catch (EmailException e) {
            log.error(e.getMessage(), e);
        }

    }
}
